package GUI;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


public class TableSelectionDemo extends JPanel implements ActionListener, Runnable
{
    private     		JTable table;
    MyTableModel 		dataModel;
    ArrayList<Child> 	tables;
    Object 				data[][];
    
    public TableSelectionDemo()
    {
        super();
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        
        Thread t = new Thread();
        t.start();
        
        tables = new ArrayList<Child>();
        
        dataModel = new MyTableModel();
        table = new JTable(dataModel);
        
        table.setColumnSelectionAllowed(false);
        table.setRowSelectionAllowed(true);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setAutoCreateRowSorter(true);
        
        this.add(new JScrollPane(table));
    }
    
    
    
    /**
     * Returns the ID of the selected row in the JTable.
     */
    public int getSelectedChild()
    {	
    
    	return tables.get(table.getSelectionModel().getLeadSelectionIndex()).getId();
    }
 
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    public void createAndShowGUI()
    {
        //Create and set up the content pane.
        TableSelectionDemo newContentPane = new TableSelectionDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
    }


    public void actionPerformed(ActionEvent event)
    {

    }

    private void outputSelection(ListSelectionEvent e)
    {
        MyTableModel m = new MyTableModel();
        SqlHandler handler = new SqlHandler();
        System.out.println("Färsk från databas: " +m.getValueAt(e.getFirstIndex(), 1).toString());
        String s = m.getValueAt(e.getFirstIndex(), 1).toString();
        System.out.println("Tilldelad till ny variabel: " + s);
        String pers = "9008542211";
        int  personnummer = Integer.parseInt(s);
        System.out.println("PARSAD: "+  personnummer);
//        System.out.println(handler.getChild(personnummer).childToString());

    }
    public Object[] printFriendNick()
    {
       MyTableModel h;
    	StringBuffer nick = new StringBuffer();
        
      //  TreeMap tm2 = new TreeMap();

        
        //tm2 =  new SqlHandler().getChildList();

     //   Set set = tm2.entrySet();
    //    Iterator it = set.iterator();

        ArrayList<Child> al = new SqlHandler().getChildList2();
        System.out.println(al.get(0).getComment());
      /*  while(it.hasNext())
        {
            Map.Entry entry = (Map.Entry) it.next();
            nick.append(entry.getKey());
            //Child temp = (Child) tm2.get(entry.getKey());
            
            al.add( tm2.get(entry.getKey()));
            
            al.get(0).getId();
            System.out.println(al.size());
            
        }
       */
        Object friends[] = al.toArray();
        dataModel.setValueAt(al.get(0),0,0);
       
        return friends;
    }
    private class RowListener implements ListSelectionListener
    {
        public void valueChanged(ListSelectionEvent event)
        {
            if(event.getValueIsAdjusting())
                return;
            event.getFirstIndex();
            outputSelection(event);
        }
    }

    private class ColumnListener implements ListSelectionListener
    {
        public void valueChanged(ListSelectionEvent event)
        {
            if (event.getValueIsAdjusting())
                return;
            outputSelection(event);
        }
    }
    

    
    public class MyTableModel extends AbstractTableModel
    {	
    	private final String [] columnNames = {"Game type:","Table name:", "Current players:"};

    	
    	
        public int getColumnCount() 
        {
            return columnNames.length;
        }

        public int getRowCount() 
        {
            return tables.size();
        }

        public String getColumnName(int col) 
        {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) 
        {
            switch (col)
            {
                case 0:
                    return tables.get(row).getName();
                case 1:
                    return tables.get(row).getSin_num();
                case 2:
                    return tables.get(row).isAttendence();           
            }
            return null;
        }
        public void setValueAt(Child value, int row, int col) 
        {
            data =new Object[1][1]; 
            data [row][col]=value.getName();
           // table = new JTable(dataModel);
           dataModel=new MyTableModel();
           // fireTableCellUpdated(row, col);
          //  table = new JTable(dataModel);
        }
     
        
    }



	@Override
	public void run() 
	{	
		
		/*Collection c = (Collection) new SqlHandler().getChildList();
		Iterator it = c.iterator();
		

		
		int col = 0;
		int row = 0;
		while(it.hasNext() && col <3)
		{
			while(col < 3)
			{
				data[row][col] = it.next().getClass().getN
						col++;
				
			}
			
		}*/
		
		
		
	}
}
    
    
    
    
    
    
    
    
    

    







		
		
		
		
		
		
		
		


